package com.baidu.logindemos.utils

import android.content.Context
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.viewbinding.ViewBinding
import com.baidu.logindemos.databinding.AlertErrorDialogBinding
import com.baidu.logindemos.databinding.AlertOkDialogBinding

class DialogUtil private constructor(){
    private lateinit var alertDialog: AlertDialog

    companion object{
        val instance: DialogUtil by lazy { DialogUtil() }
    }

    /**
     * context
     * binding
     *
     * error: title
     * btn:  hide -> Outer
     */
    fun showOK(context: Context, action:()->Unit){
        //解析视图
        val inflater = LayoutInflater.from(context)
        val binding = AlertOkDialogBinding.inflate(inflater)
        //按钮添加点击事件 将事件传递给外部
        binding.btNext.setOnClickListener {
            action()
            hide()
        }

        //创建提示框
        val dialog = AlertDialog.Builder(context)
        dialog.setView(binding.root)
        alertDialog = dialog.create()

        alertDialog.show()
    }

    fun showError(context: Context, message:String, action:()->Unit){
        //解析视图
        val inflater = LayoutInflater.from(context)
        val binding = AlertErrorDialogBinding.inflate(inflater)
        //显示提示内容
        binding.tvMessage.text = message
        //按钮添加点击事件 将事件传递给外部
        binding.btTry.setOnClickListener {
            hide()
            action()
        }

        //创建提示框
        val dialog = AlertDialog.Builder(context)
        dialog.setView(binding.root)
        alertDialog = dialog.create()

        alertDialog.show()
    }

    fun hide(){
        alertDialog.hide()
    }
}